home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / tcpdump-3.0.2 / print-icmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-15  |  6.0 KB  |  216 lines

  1. /*
  2.  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] =
  24.     "@(#) $Header: print-icmp.c,v 1.20 94/06/14 20:17:39 leres Exp $ (LBL)";
  25. #endif
  26.  
  27. #include <sys/param.h>
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31.  
  32. #include <net/if.h>
  33.  
  34. #include <netinet/in.h>
  35. #include <netinet/if_ether.h>
  36. #include <netinet/in_systm.h>
  37. #include <netinet/ip.h>
  38. #include <netinet/ip_icmp.h>
  39. #include <netinet/ip_var.h>
  40. #include <netinet/udp.h>
  41. #include <netinet/udp_var.h>
  42. #include <netinet/tcp.h>
  43. #include <netinet/tcpip.h>
  44.  
  45. #include <stdio.h>
  46.  
  47. #include "interface.h"
  48. #include "addrtoname.h"
  49.  
  50. void
  51. icmp_print(register const u_char *bp, register const u_char *bp2)
  52. {
  53.     register const struct icmp *dp;
  54.     register const struct ip *ip;
  55.     register const char *str;
  56.     register const struct ip *oip;
  57.     register const struct udphdr *ouh;
  58.     register int hlen, dport;
  59.     register const u_char *ep;
  60.     char buf[256];
  61.  
  62. #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
  63.  
  64.     dp = (struct icmp *)bp;
  65.     ip = (struct ip *)bp2;
  66.     str = buf;
  67.     /* 'ep' points to the end of avaible data. */
  68.     ep = snapend;
  69.  
  70.         (void)printf("%s > %s: ",
  71.         ipaddr_string(&ip->ip_src),
  72.         ipaddr_string(&ip->ip_dst));
  73.  
  74.     TCHECK(dp->icmp_code, sizeof(dp->icmp_code));
  75.     switch (dp->icmp_type) {
  76.     case ICMP_ECHOREPLY:
  77.         str = "echo reply";
  78.         break;
  79.     case ICMP_UNREACH:
  80.         TCHECK(dp->icmp_ip.ip_dst, sizeof(dp->icmp_ip.ip_dst));
  81.         switch (dp->icmp_code) {
  82.         case ICMP_UNREACH_NET:
  83.             (void)sprintf(buf, "net %s unreachable",
  84.                        ipaddr_string(&dp->icmp_ip.ip_dst));
  85.             break;
  86.         case ICMP_UNREACH_HOST:
  87.             (void)sprintf(buf, "host %s unreachable",
  88.                        ipaddr_string(&dp->icmp_ip.ip_dst));
  89.             break;
  90.         case ICMP_UNREACH_PROTOCOL:
  91.             TCHECK(dp->icmp_ip.ip_p, sizeof(dp->icmp_ip.ip_p));
  92.             (void)sprintf(buf, "%s protocol %d unreachable",
  93.                        ipaddr_string(&dp->icmp_ip.ip_dst),
  94.                        dp->icmp_ip.ip_p);
  95.             break;
  96.         case ICMP_UNREACH_PORT:
  97.             TCHECK(dp->icmp_ip.ip_p, sizeof(dp->icmp_ip.ip_p));
  98.             oip = &dp->icmp_ip;
  99.             hlen = oip->ip_hl * 4;
  100.             ouh = (struct udphdr *)(((u_char *)oip) + hlen);
  101.             dport = ntohs(ouh->uh_dport);
  102.             switch (oip->ip_p) {
  103.             case IPPROTO_TCP:
  104.                 (void)sprintf(buf,
  105.                     "%s tcp port %s unreachable",
  106.                     ipaddr_string(&oip->ip_dst),
  107.                     tcpport_string(dport));
  108.                 break;
  109.             case IPPROTO_UDP:
  110.                 (void)sprintf(buf,
  111.                     "%s udp port %s unreachable",
  112.                     ipaddr_string(&oip->ip_dst),
  113.                     udpport_string(dport));
  114.                 break;
  115.             default:
  116.                 (void)sprintf(buf,
  117.                     "%s protocol %d port %d unreachable",
  118.                     ipaddr_string(&oip->ip_dst),
  119.                     oip->ip_p, dport);
  120.                 break;
  121.             }
  122.             break;
  123.         case ICMP_UNREACH_NEEDFRAG:
  124.             (void)sprintf(buf, "%s unreachable - need to frag",
  125.                        ipaddr_string(&dp->icmp_ip.ip_dst));
  126.             break;
  127.         case ICMP_UNREACH_SRCFAIL:
  128.             (void)sprintf(buf,
  129.                 "%s unreachable - source route failed",
  130.                 ipaddr_string(&dp->icmp_ip.ip_dst));
  131.             break;
  132.         }
  133.         break;
  134.     case ICMP_SOURCEQUENCH:
  135.         str = "source quench";
  136.         break;
  137.     case ICMP_REDIRECT:
  138.         TCHECK(dp->icmp_ip.ip_dst, sizeof(dp->icmp_ip.ip_dst));
  139.         switch (dp->icmp_code) {
  140.         case ICMP_REDIRECT_NET:
  141.             (void)sprintf(buf, "redirect %s to net %s",
  142.                        ipaddr_string(&dp->icmp_ip.ip_dst),
  143.                        ipaddr_string(&dp->icmp_gwaddr));
  144.             break;
  145.         case ICMP_REDIRECT_HOST:
  146.             (void)sprintf(buf, "redirect %s to host %s",
  147.                        ipaddr_string(&dp->icmp_ip.ip_dst),
  148.                        ipaddr_string(&dp->icmp_gwaddr));
  149.             break;
  150.         case ICMP_REDIRECT_TOSNET:
  151.             (void)sprintf(buf, "redirect-tos %s to net %s",
  152.                        ipaddr_string(&dp->icmp_ip.ip_dst),
  153.                        ipaddr_string(&dp->icmp_gwaddr));
  154.             break;
  155.         case ICMP_REDIRECT_TOSHOST:
  156.             (void)sprintf(buf, "redirect-tos %s to host %s",
  157.                        ipaddr_string(&dp->icmp_ip.ip_dst),
  158.                        ipaddr_string(&dp->icmp_gwaddr));
  159.             break;
  160.         }
  161.         break;
  162.     case ICMP_ECHO:
  163.         str = "echo request";
  164.         break;
  165.     case ICMP_TIMXCEED:
  166.         TCHECK(dp->icmp_ip.ip_dst, sizeof(dp->icmp_ip.ip_dst));
  167.         switch (dp->icmp_code) {
  168.         case ICMP_TIMXCEED_INTRANS:
  169.             str = "time exceeded in-transit";
  170.             break;
  171.         case ICMP_TIMXCEED_REASS:
  172.             str = "ip reassembly time exceeded";
  173.             break;
  174.         }
  175.         break;
  176.     case ICMP_PARAMPROB:
  177.         if (dp->icmp_code)
  178.             (void)sprintf(buf, "parameter problem - code %d",
  179.                     dp->icmp_code);
  180.         else {
  181.             TCHECK(dp->icmp_pptr, sizeof(dp->icmp_pptr));
  182.             (void)sprintf(buf, "parameter problem - octet %d",
  183.                     dp->icmp_pptr);
  184.         }
  185.         break;
  186.     case ICMP_TSTAMP:
  187.         str = "time stamp request";
  188.         break;
  189.     case ICMP_TSTAMPREPLY:
  190.         str = "time stamp reply";
  191.         break;
  192.     case ICMP_IREQ:
  193.         str = "information request";
  194.         break;
  195.     case ICMP_IREQREPLY:
  196.         str = "information reply";
  197.         break;
  198.     case ICMP_MASKREQ:
  199.         str = "address mask request";
  200.         break;
  201.     case ICMP_MASKREPLY:
  202.         TCHECK(dp->icmp_mask, sizeof(dp->icmp_mask));
  203.         (void)sprintf(buf, "address mask is 0x%08x",
  204.             ntohl(dp->icmp_mask));
  205.         break;
  206.     default:
  207.         (void)sprintf(buf, "type-#%d", dp->icmp_type);
  208.         break;
  209.     }
  210.         (void)printf("icmp: %s", str);
  211.     return;
  212. trunc:
  213.     fputs("[|icmp]", stdout);
  214. #undef TCHECK
  215. }
  216.